﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TLWinReplace
{
    public partial class SelectDevice : Form
    {
        private string m_sTLWinDir;
        private int m_iSelectDevId;
        private string m_sDevMfg;
        private string m_sDevName;
        private string m_sDevPkg;
        private string m_sDevSizeHex;
        private string m_sDevBlankState;
        private string m_sSectorName;
        private int m_iAdapterID;

        public string SectorName
        {
            get { return m_sSectorName; }
            set { m_sSectorName = value; }
        }

        public int AdapterID
        {
            get { return m_iAdapterID; }
            set { m_iAdapterID = value; }
        }

        public string DevBlankState
        {
            get { return m_sDevBlankState; }
            set { m_sDevBlankState = value; }
        }

        public string TLWinDir
        {
            get { return m_sTLWinDir; }
            set { m_sTLWinDir = value; }
        }

        public string DevSizeHex
        {
            get { return m_sDevSizeHex; }
            set { m_sDevSizeHex = value; }
        }

        public string DevPkg
        {
            get { return m_sDevPkg; }
            set { m_sDevPkg = value; }
        }

        public string DevName
        {
            get { return m_sDevName; }
            set { m_sDevName = value; }
        }
    
        public string DevMfg
        {
            get { return m_sDevMfg; }
            set { m_sDevMfg = value; }
        }

        public int SelectDevId
        {
            get { return m_iSelectDevId; }
            set { m_iSelectDevId = value; }
        }

        public SelectDevice()
        {
            InitializeComponent();

            //// search all Flash/Managed device out
            //MessageBox.Show(m_sTLWinDir);
            //AccessDataBase adb = new AccessDataBase(m_sTLWinDir);
            //string devtype = "Flash/Managed";
            //int cnt = 0;
            //DeviceInfoElement[] dielist = new DeviceInfoElement[100];
            //cnt = adb.SearchDevicesTable(devtype, ref dielist, "");

            //dataGridView1.RowCount = 0;
            //for (int i = 0; i < cnt; i++)
            //{
            //    dataGridView1.Rows.Add(dielist[i].DevId, dielist[i].DevMfg, dielist[i].DevName, dielist[i].DevPkg, dielist[i].DevSizeHex);
            //}            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_iSelectDevId = int.Parse(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString());
            m_sDevName = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            m_sDevPkg = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            m_sDevSizeHex = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            m_sDevMfg = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            m_sDevBlankState = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            m_iAdapterID = int .Parse(dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString());
            m_sSectorName = dataGridView1[7, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            //MessageBox.Show(m_iSelectDevId.ToString());
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // search all Flash/Managed device out
            AccessDataBase adb = new AccessDataBase(m_sTLWinDir);
            // MessageBox.Show(m_sTLWinDir);
            string devtype = "Flash/Managed";
            int cnt = 0;
            DeviceInfoElement[] dielist = new DeviceInfoElement[100];
            cnt = adb.SearchDevicesTable(devtype, ref dielist, textBox1.Text);

            dataGridView1.RowCount = 0;
            for (int i = 0; i < cnt; i++)
            {
                dataGridView1.Rows.Add(dielist[i].DevId, dielist[i].DevMfg, dielist[i].DevName,
                                        dielist[i].DevPkg, dielist[i].DevSizeHex, dielist[i].DevBlankState,
                                        dielist[i].AdapterID, dielist[i].SectorTableName);
            }
        }
        
    }
}
